/***************************************************************************\
 * The Mattise Kernel														*
 * Copyright 2007 Matthew Iselin											*
 * THIS FILE is Public Domain												*
 *																			*
 * fat32.h																	*
 *																			*
 * Handles FAT32 filesystem reads and writes. Currently also contains		*
 * an implementation of standard functions that should be controlled 		*
 * elsewhere.																*
 *																			*
\***************************************************************************/

#ifndef FILESYS_H
#define FILESYS_H

#include <system.h>

// boot parameter block for all FAT filesystems
struct BPB_FAT
{
	uchar_t		BS_jmpBoot[3];
	uchar_t		BS_OEMName[8];
	ushort_t	BPB_BytsPerSec;
	uchar_t		BPB_SecPerClus;
	ushort_t	BPB_RsvdSecCnt;
	uchar_t		BPB_NumFATs;
	ushort_t	BPB_RootEntCnt;
	ushort_t	BPB_TotSec16;
	uchar_t		BPB_Media;
	ushort_t	BPB_FATSz16;
	ushort_t	BPB_SecPerTrk;
	ushort_t	BPB_NumHeads;
	uint_t		BPB_HiddSec;
	uint_t		BPB_TotSec32;
} __attribute((packed));

// initializes FAT32
void InitFat32();

// initialize the BPB for a drive (0 is usually IDE master)
void InitBPB_FAT32( int drive );

// prints out the contents of the root directory
void PrintRootDir_FAT32( int drive );

// FAT32 directory entry
struct FAT32_DirEnt {
	uchar_t		DIR_Name[11];
	uchar_t		DIR_Attr;
	uchar_t		DIR_NTRes;
	uchar_t		DIR_CrtTimeTenth;
	ushort_t	DIR_CrtTime;
	ushort_t	DIR_CrtDate;
	ushort_t	DIR_LstAccDate;
	ushort_t	DIR_FstClusHI;
	ushort_t	DIR_WrtTime;
	ushort_t	DIR_WrtDate;
	ushort_t	DIR_FstClusLO;
	uint_t		DIR_FileSize;
};

// file type
typedef struct {
	uint_t dev,devdesc;
	uint_t dirsect,diroffset;
	struct FAT32_DirEnt myDirEnt;
} fat32_filedesc;

#endif
